package cn.zh.service.impl;

import cn.zh.commons.ResponseResult;
import cn.zh.entity.Houseresource;
import cn.zh.mapper.ResourceMapper;
import cn.zh.service.ResourceService;
import cn.zh.vo.UserVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author : 张航
 * @date : 2024-05-30 15:16
 * @description:
 **/
@Service
public class ResourceServiceImpl implements ResourceService {
    @Resource
    private ResourceMapper resourceMapper;


    public ResponseResult page(Integer page, Integer limit, String pattern) {
        PageHelper.startPage(page,limit);

        List<Houseresource> houseresources=resourceMapper.serach(pattern);

        long total = new PageInfo<Houseresource>(houseresources).getTotal();

        return ResponseResult.success().count(total).data(houseresources);
    }


    public ResponseResult houseUsername(String houseUsername) {
        UserVo userVo =resourceMapper.userVo(houseUsername);
        return ResponseResult.success().data(userVo);
    }

    //完成审核功能

    public ResponseResult audit(UserVo userVo) {
        resourceMapper.audit(userVo);
        return ResponseResult.success().message("已审核");
    }


    public ResponseResult delete(Integer id) {
        resourceMapper.delete(id);
        return ResponseResult.success().message("删除成功");
    }


    public ResponseResult count() {
       Long counts= resourceMapper.count();
        return  ResponseResult.success().data(counts);
    }


    public ResponseResult listup(Integer page,Integer limit,String pattern) {
        PageHelper.startPage(page,limit);

        List<Houseresource> houseresources=resourceMapper.listup(pattern);

        long total = new PageInfo<Houseresource>(houseresources).getTotal();

        return ResponseResult.success().count(total).data(houseresources);
    }


    public ResponseResult listdown(Integer page, Integer limit, String pattern) {
        PageHelper.startPage(page,limit);

        List<Houseresource> houseresources=resourceMapper.listdown(pattern);

        long total = new PageInfo<Houseresource>(houseresources).getTotal();

        return ResponseResult.success().count(total).data(houseresources);
    }


    public ResponseResult list(Integer page, Integer limit, String pattern) {
        PageHelper.startPage(page,limit);

        List<Houseresource> houseresources=resourceMapper.list(pattern);

        long total = new PageInfo<Houseresource>(houseresources).getTotal();

        return ResponseResult.success().count(total).data(houseresources);
    }


    public ResponseResult byId(Integer id) {
        Houseresource houseresource=resourceMapper.byId(id);
        return ResponseResult.success().data(houseresource);
    }

    public ResponseResult publish(Houseresource houseresource) {

        resourceMapper.insert(houseresource);

        return ResponseResult.success().message("发布成功");
    }

    public ResponseResult UserPage(Integer page, Integer limit, String pattern, String username) {

        PageHelper.startPage(page,limit);

        List<Houseresource> houseresources=resourceMapper.serachHouseByUsername(pattern,username);

        long total = new PageInfo<Houseresource>(houseresources).getTotal();

        return ResponseResult.success().count(total).data(houseresources);

    }
}
